/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.settings;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import ch.bfh.aeneas.R;
import ch.bfh.aeneas.core.io.gesture.GestureBuilder;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;

/**
 *
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class SettingsChooser extends Activity {

    private final static String LOG_TAG = "SettingsChooser";
    
    protected boolean mProcessing;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        mProcessing = false;
    }
    
    @Override
    public void onResume() {
        super.onResume();
        
        Log.i(LOG_TAG, "onResume");
        if(!mProcessing) {
            Intent pick = new Intent(AeneasIntent.ACTION_SELECT);
            pick.addCategory(AeneasIntent.CATEGORY_STRING);
            try {
                pick.putExtra("values", getResources().getStringArray(R.array.settings_modes_array));
            } catch (Exception ex) {
                Log.e(LOG_TAG, "No settings-modes found!");
                finish();
            }
            pick.putExtra("title", getString(R.string.main_settings));

            startActivityForResult(pick, 1);
        } else {
            mProcessing = false;
        }
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1) {
            if(resultCode == Activity.RESULT_OK) {
                
                int index = data.getIntExtra("selectedIndex",-1);
                Intent intent;
                
                Log.i(LOG_TAG, "User picked index "+index);
                
                if(index == 0) {
                    Log.i(LOG_TAG, "Starting Preferences");
                    intent = new Intent(this, Preferences.class);
                } else {
                    Log.i(LOG_TAG, "Starting GesturePreferences");
                    intent = new Intent(this, GestureBuilder.class);
                }
                
                mProcessing = true;
                
                startActivity(intent);
            } else {
                Log.i(LOG_TAG, "Action was cancel - so closing");
                finish();
            }
        }
    }
}
